package com.orhon.smartcampus.modules.notice.fetcher;

import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.notice.dao.NoticeDao;
import com.orhon.smartcampus.modules.notice.entity.Notice;
import com.orhon.smartcampus.modules.notice.gqlModel.GQLNotice;
import com.orhon.smartcampus.modules.notice.mm.NoticeMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import com.orhon.smartcampus.utils.R;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class NoticeFetcher implements DataFetcher {

    @Autowired
    NoticeDao noticeDao;

    @Autowired
    TokenService tokenService;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
        String token = tokenService.getSystemToken();

        if(token==null) {
            return  R.NoLogin();
        }
//        Long schoolId = JwtUtils.getSchoolId(token);

        int id = Integer.parseInt(env.getArgument("id").toString());

        String hql = "from Notice where deletedAt = null and id = "+id;
//        String hql = "from Notice where deleted_at = null and id = "+id+" and school_id = " + schoolId;
        Notice item = noticeDao.findId(hql, null);
//        TInformation item =  tInformationDao.findId(hql, null);
        if(item!=null) {


            GQLSchool gqlSchool = null;
            if (env.getSelectionSet().contains("school")){
                Schools schools = item.getBaseSchoolsBySchoolId();
                gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
            }

            List<GQLSchool> gqlSchools = null;
            if (env.getSelectionSet().contains("schools")){
                List<Schools> schoolsList = item.getSchools();
                gqlSchools = SchoolMapper.MAPPER.toSchoolList(schoolsList);
                System.out.println("接受人:"+gqlSchools.toString());
            }

            GQLNotice notice = NoticeMapper.MAPPER.toNotice(item);
            notice.setSchool(gqlSchool);
            notice.setSchools(gqlSchools);

            return notice;
        }
        return null;

    }
}
